package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.Login;
import com.example.mall.domain.UserMemberships;
import com.example.mall.dto.UserMembershipsDto;
import com.example.mall.mapper.UserMembershipsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserMembershipsQuery;
import com.example.mall.service.IUserMembershipsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户会员表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class UserMembershipsServiceImpl extends ServiceImpl<UserMembershipsMapper, UserMemberships> implements IUserMembershipsService {

    @Autowired
    private UserMembershipsMapper userMembershipsMapper;

    @Override
    public PageDTO<UserMembershipsDto> queryPage(UserMembershipsQuery userMembershipsQuery) {
        if (userMembershipsQuery == null) {
            userMembershipsQuery = new UserMembershipsQuery();
        }
        Page<UserMemberships> page = userMembershipsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserMemberships> cartPage = lambdaQuery()
                .like(userMembershipsQuery.getMembershipId()!= null, UserMemberships::getMembershipId, userMembershipsQuery.getMembershipId())
                .like(userMembershipsQuery.getUserId()!= null, UserMemberships::getUserId, userMembershipsQuery.getUserId())
                .like(userMembershipsQuery.getLevelId()!= null, UserMemberships::getLevelId, userMembershipsQuery.getLevelId())
                .apply(userMembershipsQuery.getStartTime() != null,
                        "DATE_FORMAT(start_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userMembershipsQuery.getStartTime())
                .apply(userMembershipsQuery.getEndTime() != null,
                        "DATE_FORMAT(end_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userMembershipsQuery.getEndTime())
                .orderByDesc(UserMemberships::getUpdateTime)
                .page(page);
        return PageDTO.of(cartPage, UserMembershipsDto.class);
    }

    @Override
    public UserMemberships queryUserMembershipsById(Long membershipId) {
        AssertUtil.isNotNull(membershipId, "会员ID不能为空");
        return userMembershipsMapper.selectById(membershipId);
    }

    @Override
    public List<UserMemberships> queryUserMembershipsList() {
        return this.list();
    }

    @Override
    public void insert(UserMembershipsDto userMembershipsDto) {
        UserMemberships userMemberships = new UserMemberships();
        CopyUtils.copyProperties(userMembershipsDto, userMemberships);
        userMemberships.setMembershipId(CodeGenerateUtils.generateUniqueId());
        userMemberships.setCreateTime(new Date());
        userMemberships.setUpdateTime(new Date());
        userMembershipsMapper.insert(userMemberships);
    }

    @Override
    public void update(UserMembershipsDto userMembershipsDto) {
        AssertUtil.isNotNull(userMembershipsDto.getMembershipId(), "会员ID不能为空");
        UserMemberships userMemberships = userMembershipsMapper.selectById(userMembershipsDto.getMembershipId());
        AssertUtil.isNotNull(userMemberships, "会员信息不存在");
        CopyUtils.copyProperties(userMembershipsDto, userMemberships);
        userMemberships.setUpdateTime(new Date());
        userMembershipsMapper.updateById(userMemberships);
    }

    @Override
    public void delete(Long membershipId) {
        AssertUtil.isNotNull(membershipId, "会员ID不能为空");
        UserMemberships userMemberships = userMembershipsMapper.selectById(membershipId);
        AssertUtil.isNotNull(userMemberships, "会员信息不存在");
        userMembershipsMapper.deleteById(membershipId);
    }

    @Override
    public void deleteBatch(List<Long> membershipIds) {
        AssertUtil.isNotNull(membershipIds, "会员ID列表不能为空");
        List<UserMemberships> userMemberships = userMembershipsMapper.selectBatchIds(membershipIds);
        AssertUtil.isNotNull(userMemberships, "会员信息不存在");
        userMembershipsMapper.deleteBatchIds(membershipIds);
    }
}
